package com.macro.mall.portal.util;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.StorageClass;
import com.qcloud.cos.region.Region;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;


public class Upload {
    public static void main(String[] args) {
        URL upload = upload();
        System.out.println(upload);
    }


    public static URL upload() {
        // 1 初始化用户身份信息API密钥(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials("AKIDRhhRu63j8G8j36uRhgPs4zd06vTlmWzB", "xd3E9nBI9l7D2arqZ2E9nBH6evve6HCq");
        // 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
//        ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing"));
        ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
        // 3 生成cos客户端
        COSClient cosclient = new COSClient(cred, clientConfig);
        // 存储桶bucket名需包含appid
//        String bucketName = "demo-1258118289";1301967145
        String bucketName = "images-1301967145";
        // 指定要上传到 COS 上对象键
        // 对象键（Key）是对象在存储桶中的唯一标识。例如，在对象的访问域名 `bucket1-1250000000.cos.ap-guangzhou.myqcloud.com/doc1/pic1.jpg` 中，对象键为 doc1/pic1.jpg, 详情参考 [对象键](https://cloud.tencent.com/document/product/436/13324)
        String key = "2.png";
//        File localFile = new File("src/main/resources/2.png");
        File localFile = new File("/Users/duanbiao/Desktop/1221629299897.png");
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        // 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
        putObjectRequest.setStorageClass(StorageClass.Standard_IA);
        URL url = null;
        try {
            PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);

            Date expiration = new Date(new Date().getTime() + 5 * 60 * 10000);
            url = cosclient.generatePresignedUrl(bucketName, key, expiration);
            System.out.println("图片在COS服务器上的url:" + url);

            // putobjectResult会返回文件的etag
            String etag = putObjectResult.getETag();

        } catch (CosServiceException e) {
            e.printStackTrace();
        } catch (CosClientException e) {
            e.printStackTrace();
        }
        // 关闭客户端
        cosclient.shutdown();
        return url;
    }


}
